<!-- 工作量统计表 -->
<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form size="mini" label-width="80px">
        <el-row class="bg-row">
          <el-form-item label="查询分类">
            <el-radio-group v-model="spreadData.classify">
              <el-radio label="1">销售日报表</el-radio>
              <el-radio label="2">销售月报表</el-radio>
              <el-radio label="3">进销存报表</el-radio>
              <el-radio label="4">进销存汇总表</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row class="bg-row"  v-if="spreadData.classify != 4">
          <el-form-item label="所属公司" v-if="[1,21,23].includes(userInfo.lessDeptId)">
            <el-select size="mini" style="width: 175px;" v-model="spreadData.unitId" @change="unitChangeHandle">
              <el-option v-for="item in companyOption" :value="item.id" :label="item.name" :key='item.id'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属公司" v-else>
            <el-select size="mini" style="width: 175px;" v-model="spreadData.unitId" disabled>
              <el-option v-for="item in companyOption" :value="item.id" :label="item.name" :key='item.id'></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="bg-row">
          <el-col :span="9" v-if="spreadData.classify != 4">
            <el-form-item label="查询部门">
              <el-input size="mini" v-model="spreadData.deptName" v-if="userInfo.lessDeptId != '23'" disabled style="width: 175px;"></el-input>
              <el-input size="mini" type="textarea" v-model="spreadData.deptName" v-if="[1,21,23].includes(userInfo.lessDeptId)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3" v-if="spreadData.classify != 4">
            <el-form-item label-width="0" v-if="[1,21,23].includes(userInfo.lessDeptId)">
              <el-button size="mini" @click="openDeptDialog">选择</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="查询日期">
                <el-col :span="11" v-if="spreadData.classify <= 2">
                  <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.beginTime" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                </el-col>
              <el-col class="line" :span="2" style="text-align: center;" v-if="spreadData.classify == 2">至</el-col>
              <el-col :span="11" v-if="spreadData.classify == 2">
                <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.endTime" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-col>
                <el-col :span="12" v-if="spreadData.classify == 3">
                  <el-date-picker
                    v-model="dataForm.yearMonth"
                    class="timePicker"
                    type="month"
                    placeholder="选择日期"
                    format="yyyy-MM"
                    value-format="yyyy-MM">
                  </el-date-picker>
                </el-col>
              <el-col :span="12" v-if="spreadData.classify == 4">
                <el-date-picker
                  v-model="dataForm.yearMonth"
                  class="timePicker"
                  type="month"
                  placeholder="选择日期"
                  format="yyyy-MM"
                  value-format="yyyy-MM">
                </el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="bg-row" v-if="spreadData.classify >= 3">
          <el-form-item label="商品类别">
            <el-radio-group v-model="dataForm.ctgId">
              <el-radio label="0">全部</el-radio>
              <el-radio label="1">镜架</el-radio>
              <el-radio label="2">配件</el-radio>
              <el-radio label="3">镜片</el-radio>
              <el-radio label="4">隐形</el-radio>
              <el-radio label="5">护理液</el-radio>
              <el-radio label="6">太阳镜</el-radio>
              <el-radio label="7">耗材</el-radio>
              <el-radio label="8">老花镜</el-radio>
              <el-radio label="9">视光</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row class="bg-row">
          <el-col :span="12">
            <el-form-item label="显示查询条件" label-width="100px">
              <el-radio-group v-model="spreadData.condition">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="2">否</el-radio>
                </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">
            <el-link  :href="printUrl" target="_blank" type="primary"><el-button size="mini" type="primary" @click="getWorkParams" v-prevent-re-click>查 询</el-button></el-link>
            <el-button type="info" icon="el-icon-delete" style="margin-left: 15px;">清 空</el-button>
          </el-form-item>
        </el-row>

      </el-form>
    </basic-container>
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData" :company="this.spreadData.unitId"></search-dept>
  </div>
</template>

<script>
  let params = '/work/saleByUser.cpt'
  import { getDateFormat } from '@/util/tools'
  import { filterParams } from '@/util/tools'
  import { mapGetters } from 'Vuex'

  //  查询公司列表
  import { unitList } from '@/api/admin/unit'
  import { getDeptPage } from '@/api/admin/dept'


  import SearchDept from '@/components/business/SearchDept'


  export default {
      data () {
        return {
          radio: 3,
          printUrl: '',
          companyOption: [],
          spreadData: {
            classify: '1',    //  查询分类
            mode: '2',        //  查询方式
            checkType: '1',   //  查看方式
            unitId: '',      //  所属公司
            deptName: '',     //  查询部门Name
            condition: '2',    //  是否显示查询条件
          },
          dataForm: {
            deptId: '',       //  查询部门ID
            beginTime: '',    //  查询开始时间
            endTime: '',      //  查询结束时间
            jobNumber: '',    //  工号
            yearMonth: '',    //  时间年月
            lastMonth: '',
          },
          deptVisible: false
        }
      },
      components: {
        SearchDept
      },
      computed: {
        ...mapGetters(['userInfo','bizData'])
      },
      created() {
        this.dataForm.beginTime = getDateFormat(new Date())
        this.dataForm.endTime = getDateFormat(new Date())
        this.dataForm.yearMonth = getDateFormat(new Date())
        this.getCompanyList()
        this.spreadData.unitId = this.userInfo.unitId
        this.spreadData.deptId = [this.userInfo.lessDeptId]
        this.spreadData.deptName = this.userInfo.lessDeptName
      },
      methods:{
        getFormData() {
          // this.dataForm.deptId = this.userInfo.lessDeptId
          // this.spreadData.deptName = this.userInfo.lessDeptName
        },
        unitChangeHandle(val){
          let ids = []
          let names = []
          getDeptPage({current: 1,size: 100,unitId: val}).then(result => {
            let resultData = result.data.data.records

            resultData.forEach(item => {
              ids.push(item.id)
              names.push(item.name)
            })
            this.spreadData.deptName = names.join(',')
            this.spreadData.deptId = ids
            this.dataForm.deptId = ids
          })
        },
        getWorkParams() {
            if(this.spreadData.classify == '1'){
                params = '/JXCtotal/sale_saleByDay.cpt'
            }else if(this.spreadData.classify == '2'){
                params = '/JXCtotal/sale_saleByMonth.cpt'
            }else if(this.spreadData.classify == '3') {
                params = '/JXCtotal/sale_storageByMonth.cpt'
            }else if(this.spreadData.classify == '4'){
                    params = '/JXCtotal/sale_storageByMonth-collect.cpt'
                }
            if(this.spreadData.deptId){
                this.dataForm.deptId=this.spreadData.deptId
            }
            if(this.dataForm.ctgId == '0'){
                this.dataForm.ctgId = ''
            }
            if(this.dataForm.yearMonth){
                let lastMonth = this.getLastMonth(this.dataForm.yearMonth);
                this.dataForm.lastMonth = lastMonth;
            }
          let data = filterParams(this.dataForm)
          let parameter = ''
          for(let key in data){
            parameter += '&' + key + '=' + this.dataForm[key]
          }
          // let parameter = '&deptId='+this.dataForm.deptId + '&beginTime='+this.dataForm.beginTime+ '&endTime='+this.dataForm.endTime
          //let frUrl = "http://localhost:8075/webroot/decision/view/report?viewlet="
          //this.printUrl = frUrl + params + parameter
          this.printUrl = this.bizData.printUrl + params + parameter
        },
        //  查询公司列表
        getCompanyList() {
          unitList().then(result => {
            this.companyOption = result.data.data
          })
        },
        //  打开查询部门弹窗
        openDeptDialog() {
          this.deptVisible = true
          this.$nextTick(() => {
            this.$refs.deptDialog.init()
          })
        },
        //  获取选择部门数据
        getDeptData(data) {
          let ids = []
          let names = []
          if(data.length){
            data.forEach(item => {
              ids.push(item.id)
              names.push(item.name)
            })
          }
          this.spreadData.deptName = names.join(',')
          this.spreadData.deptId = ids
          this.dataForm.deptId = ids
        },
        getLastMonth(date) {
          let year = date.substr(0,4);   //当前年：四位数字
          let month = date.substr(5,2);      //当前月：0-11
          month =  parseInt(month);
          console.log(month);
          if (month == 1) {   //如果是0，则说明是1月份，上一个月就是去年的12月
              year -= 1;
              month = 12;
          }else{
              month = month-1;
              console.log(month);
          }
          month = month < 10 ? ('0' + month) : month;   //月份格式化：月份小于10则追加个0
          let lastYearMonth = year + '-' + month;
          console.log(lastYearMonth);
          return lastYearMonth;
      }
      }
    }
</script>
<style lang="scss" scoped>
  .bg-row {
    // background-color: #9ec0b0;
    padding-right: 15px;
    padding-top: 5px;
    margin-bottom: 2px;
  }
</style>
